package cn.cloudplug.ui.widget;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageButton;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;

import java.util.List;

import cn.cloudplug.core.index.ConfigKeys;
import cn.cloudplug.core.index.Scent;
import cn.cloudplug.core.util.MyUtil;
import cn.cloudplug.ui.view.SuspensionFrame;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by pc on 2018/2/2.
 */

public class SuspensionButton2 implements View.OnClickListener, FloatingActionMenu.MenuStateChangeListener, SuspensionFrame.OnResumeListener {
    private final static String TAG="SuspensionButton";
    private static final int RADIUS = 5;
    private static final int MENU_RADIUS = 353;
    private AppCompatActivity activity;
    private final static int WIDTH=100;
    private final static int HEIGHT=100;
    private final static int MARGIN_BOTTOM=25;
    private final static int MARGIN_RIGHT=25;
    private boolean isShow=false;
    private List<SuspensionButton2.Action> buttons;
    private SuspensionFrame mDialog;
    private CircleImageView mainButton;
    private ViewGroup rootGroup;
    public SuspensionButton2(AppCompatActivity activity, List<SuspensionButton2.Action> buttons){
        this.activity=activity;
        this.buttons=buttons;
        rootGroup = (ViewGroup) activity.getWindow().getDecorView();
        initMainButton();
        initMainDialog();
    }

    private void initMainDialog() {
        mDialog = new SuspensionFrame();
        mDialog.setOnResumeListener(this);
    }

    private void initMainButton() {
        mainButton=new CircleImageView(activity);
        mainButton.setBackground(null);
        mainButton.setOnClickListener(this);
        showMainButtonToActivity();
    }

    public void setImage( int id){
        mainButton.setImageResource(id);
    }

    private void showMainButtonToActivity() {
        ViewGroup.LayoutParams ibtnlp = new ViewGroup.LayoutParams(WIDTH,HEIGHT);
        int[] screenSize = Scent.getConfiguration(ConfigKeys.ScreenSize);
        mainButton.setTranslationY(screenSize[1]-HEIGHT-MARGIN_BOTTOM);
        mainButton.setTranslationX(screenSize[0]-WIDTH-MARGIN_RIGHT);
        rootGroup.addView(mainButton,ibtnlp);
    }

    private View createImage(int imgId) {
        ImageButton  imageButton = new AppCompatImageButton(activity);
        imageButton.setImageResource(imgId);
        return  imageButton;
    }

    public static SuspensionButton2 create(AppCompatActivity activity, List<Action> buttons) {
        return new SuspensionButton2(activity,buttons);
    }

    @Override
    public void onClick(View v) {
        mDialog.setBgBitmap(MyUtil.loadBitmapFromViewBySystem(rootGroup),RADIUS);
        mDialog.show(activity.getSupportFragmentManager(),TAG);
        Toast.makeText(activity,"onClick",Toast.LENGTH_SHORT).show();
    }

    public void hide() {
        mainButton.setVisibility(View.GONE);
        isShow=false;
    }

    public void show() {
        mainButton.setVisibility(View.VISIBLE);
        isShow=true;
    }

    @Override
    public void onMenuOpened(FloatingActionMenu menu) {
        Toast.makeText(activity,"onMenuOpened",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onMenuClosed(FloatingActionMenu menu) {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onStart() {
        ViewGroup vg=(ViewGroup)mainButton.getParent();
        if(vg!=null)vg.removeView(mainButton);
        mDialog.getViewGroup().addView(mainButton);
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onStop() {
        ViewGroup vg=(ViewGroup)mainButton.getParent();
        if(vg!=null)vg.removeView(mainButton);
        if(isShow)showMainButtonToActivity();
    }

    public static class Action{
        private String title;
        private int imgId;

        public Action(String title, int imgId) {
            this.title = title;
            this.imgId = imgId;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getImgId() {
            return imgId;
        }

        public void setImgId(int imgId) {
            this.imgId = imgId;
        }
    }
}
